package lib_model.Account;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import lib_dal.Connection.ConnectMSSQL;
import lib_dal.Interface.ISql;
import lib_model.Orther.ReturnObj;

public class Account_group implements ISql {

	public int id;
	public String group_name;
	public String description;
	public Date date_create;

	
	
	public Account_group(int id) {
		super();
		this.id = id;
	}

	public Account_group() {
	}

	@Override
	public ReturnObj Select() {
		String sql = "select id, group_name, description, date_create from dbo.account_group";
		ReturnObj obj = new ReturnObj();
		ArrayList<Account_group> arr = new ArrayList<Account_group>();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Account_group g = new Account_group();
				g.id = rs.getInt("id");
				g.date_create = rs.getDate("date_create");
				g.description = rs.getString("description");
				g.group_name = rs.getString("group_name");
				arr.add(g);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		obj.ObjResult = arr;
		return obj;
	}

	@Override
	public ReturnObj Add() {
		String sql = "insert into dbo.account_group (  group_name,   description,   date_create) values   (   ?,   ?,   GETDATE())";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setString(1, this.group_name);
			preState.setString(2, this.description);
			obj.Result = (preState.executeUpdate() != 0) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Update() {
		String sql = "update dbo.account_group set group_name = ?,   description = ? where  id = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setString(1, this.group_name);
			preState.setString(2, this.description);
			preState.setInt(3, this.id);
			obj.Result = (preState.executeUpdate() != 0) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Delete() {
		String sql = "delete from dbo.account_group where id = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj GetInformation() {
		ReturnObj obj = new ReturnObj();
		String sql = "select id, group_name, description, date_create from dbo.account_group where id = ?";
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Account_group g = new Account_group();
				g.id = rs.getInt("id");
				g.date_create = rs.getDate("date_create");
				g.description = rs.getString("description");
				g.group_name = rs.getString("group_name");
				obj.ObjResult = g;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

}
